const {
	Service
} = require('uni-cloud-router')
const {empty,typeOf} = require('../methods/index.js')

module.exports = class chooseService extends Service {
	constructor(ctx) {
		super(ctx)
		this.dbCmd = this.db.command
		this.hrCourse = this.db.collection('hr-course')
		this.hrCollege = this.db.collection('hr-college')
		this.teachingClass = this.db.collection('teaching-class')
		this.hrUserSchedule = this.db.collection('hr-userSchedule')
		const date = new Date()
		this.year = date.getFullYear()
		this.term = date.getMonth() + 1 < 7 ? 0 : 1 
		this.examChange = function(exam){
			let res = ''
			switch(exam){
				case 0:
					res = '考试课'
					break
				case 1:
					res = '考试课'
					break
				case 2:
					res = '在线课'
					break
				default:
					res = '未知'
			}
			return res
		}
	}
	
	/**
	 * 获取学院
	 */
	async getCollege(){
		let college = []
		return this.hrCollege.orderBy("sort","asc").get().then((res)=>{
			// 插入学院
			res.data.map((item)=>{
				college.push({
					id: item.college_id,
					label:item.college_name,
					children:[]
				})
			})
			return college
		})
	}
	
	/**
	 * 获取课程
	 */
	async getCourse(format){
		if(typeOf(format)==='object'){
			return this.hrCourse.where(format,{status:0}).get()
		}else{
			return this.hrCourse.where({status:0}).get()
		}
	}
	
	/**
	 * 已选的课程
	 */
	async selectedCourse(id){
		let selectC = [] , count = 0 , length = 0
		return new Promise((resolve,reject)=>{
			function isEnd(){
				if(++count === length){
					resolve(selectC)
				}
			}
			this.hrUserSchedule.where({year:this.year,username:id}).get().then((res)=>{
				if(res.affectedDocs === 0){
					resolve(selectC)
				}
				length = res.data[0].schedule.length
				if(length === 0){
					resolve(selectC)
				}
				res.data[0].schedule.map(async (key)=>{
					
					let {data} = await this.service.choose.getCourse({course_id:key.course_id})
					selectC.push({
						id: data[0].course_id,
						label:data[0].course_name,
						comment:data[0].comment,
						prerequisite_name:data[0].prerequisite_name,
						credit:data[0].credit,
						exam:this.examChange(data[0].exam)
					})
					isEnd()
				})
			}).catch((err)=>{
				reject(err)
			})
		})
	}
	
	/**
	 * 剔除课程
	 */
	async filterCourse(id){
		let selectC = await this.service.choose.selectedCourse(id)
		let {data}  = await this.service.choose.getCourse()
		if(empty(selectC)){
			return data
		}
		selectC.map((item)=>{
			data = data.filter((key)=>{
				return key.course_id !== item.id
				})
		})
		return data
	}
	
	/**
	 * 课程目录
	 */
	async courseCatalogue({id}){
		let data = await this.service.choose.getCollege()
		await this.service.choose.filterCourse(id).then((res)=>{
			console.log(res)
			res.map((item)=>{
				data[item.college-1].children.push({
					id: item.course_id,
					label:item.course_name,
					comment:item.comment,
					prerequisite_name:item.prerequisite_name,
					credit:item.credit,
					exam:this.examChange(item.exam)
				})
			})
		})
		return await this.service.choose.selectedCourse(id).then((res)=>{
			res.forEach(item => {
				data.unshift(item)
			})
			return data
		})
	}
}